package lily.phs.vaccination;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import lily.phs.child.ChildScrollActivity;
import lily.phs.child.ImageList;
import lily.phs.database.Constants;
import lily.phs.entertain.YuleActivity;
import lily.phs.epidemic.EpidemicList;
import lily.phs.healthedu.HealthMain;
import lily.phs.object.ModuleSelect;
import lily.phs.pregnance.PregnantScrollActivity;
import lily.phs.urh.PeopleScrollActivity;
import lily.phs.urh.R;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class VaccinationScrollActivity extends Activity  implements OnClickListener,Constants{

    private static final String[] nameOfModuleList = {
    	"城乡居民健康档案管理","健康教育","疫情上报","卫生监督","寓教于乐"};
    private static final int[] imageOfModuleList = {
    	R.drawable.peoplehealth,R.drawable.health_education,R.drawable.yiqingshangbao,
    	R.drawable.weishengjiandu,R.drawable.entertain};
    
    private static final String[] vaccinationTable={"预防接种卡","预防接种记录"};
    int image[];
    String name[];
	private VaccinationCardView vaca;
	private VaccinationRecordView vare;
	private VaccinationRecordView2 vare2;
	
	private Button leftBtn,rightBtn,rightBtn2;
	private ListView listAccount;
	private GridView gv2;
	private int profileId=0,father,call;
	private int pageIndex;
	private ProgressDialog mpDialog;
	VaccinationScrollLayout mRoot;
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
        setContentView(R.layout.vaccination);
        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.i_title);
        leftBtn=(Button) findViewById(R.id.title_left_btn);
        rightBtn=(Button) findViewById(R.id.title_right_btn);
        rightBtn2=(Button) findViewById(R.id.title_right_btn2);
        leftBtn.setOnClickListener(this);
        rightBtn.setOnClickListener(this);
        rightBtn2.setOnClickListener(this);
        leftBtn.setText("后退");
        rightBtn.setText("保存");
        rightBtn2.setText("提交");
        mRoot=(VaccinationScrollLayout)findViewById(R.id.root);
        listAccount=(ListView)findViewById(R.id.listAccount);
        initList();
        
        profileId=getIntent().getExtras().getInt("Profile_id");
        System.out.println("getIntent().getExtras().getInt(Profile_id);"+getIntent().getExtras().getInt("Profile_id"));
        father=getIntent().getExtras().getInt("father");
        call=getIntent().getExtras().getInt("call");
        initViews();
        mRoot.setToScreen(0);
        pageIndex=0;
        listAccount.setOnItemClickListener(new OnItemClickListener(){

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int index,
					long arg3) {
				// TODO Auto-generated method stub
				switch(index){
				case 0:
					mRoot.setToScreen(0);
					break;
				case 1:
					mRoot.setToScreen(1);
					break;
				case 2:
					mRoot.setToScreen(2);
					break;
				
				}
			}
        });
        
        gv2=(GridView)findViewById(R.id.gride2);
        ImageList adapter2 = new ImageList(this,imageOfModuleList,nameOfModuleList);
		gv2.setAdapter(adapter2);
		gv2.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				switch (arg2) {
				case 0:{//健康档案
					if(father==MODULE_FATHER){
						Intent editprofile = new Intent();
						Bundle bd = new Bundle();
						bd.putInt("Profile_id", profileId);//待编辑档案的ID
						bd.putInt("father", MODULE_CHILD);
						bd.putInt("call", MODULE_VACCINATION);
						editprofile.putExtras(bd);
						editprofile.setClass(VaccinationScrollActivity.this, PeopleScrollActivity.class);
						startActivity(editprofile);
						overridePendingTransition(R.anim.anim_slide_in_right,R.anim.anim_slide_out_left);
					}
					else if(father==MODULE_CHILD){
						if(call==MODULE_PEOPLE_HEALTH){
							finish();
							overridePendingTransition(R.anim.anim_slide_in_left,R.anim.anim_slide_out_right);
						}
						else{
							Intent editprofile = new Intent();
							Bundle bd = new Bundle();
							bd.putInt("Profile_id", profileId);//待编辑档案的ID
							bd.putInt("father", MODULE_CHILD);
							bd.putInt("call", call);
							editprofile.putExtras(bd);
							editprofile.setClass(VaccinationScrollActivity.this, PeopleScrollActivity.class);
							startActivity(editprofile);
							overridePendingTransition(R.anim.anim_slide_in_right,R.anim.anim_slide_out_left);
							finish();
						}
					}
					break;
				}
				case 1:{//健康教育
					if(father==MODULE_FATHER){
						Intent editprofile = new Intent();
						Bundle bd = new Bundle();
						bd.putInt("Profile_id", profileId);//待编辑档案的ID
						bd.putInt("father", MODULE_CHILD);
						bd.putInt("call", MODULE_PEOPLE_HEALTH);
						editprofile.putExtras(bd);
						editprofile.setClass(VaccinationScrollActivity.this, HealthMain.class);
						startActivity(editprofile);
						overridePendingTransition(R.anim.anim_slide_in_right,R.anim.anim_slide_out_left);
					}
					else if(father==MODULE_CHILD){
						if(call==MODULE_CHILD_MANAGEMENT){
							finish();
							overridePendingTransition(R.anim.anim_slide_in_left,R.anim.anim_slide_out_right);
						}
						else{
							Intent editprofile = new Intent();
							Bundle bd = new Bundle();
							bd.putInt("Profile_id", profileId);//待编辑档案的ID
							bd.putInt("father", MODULE_CHILD);
							bd.putInt("call", call);
							editprofile.putExtras(bd);
							editprofile.setClass(VaccinationScrollActivity.this, HealthMain.class);
							startActivity(editprofile);
							overridePendingTransition(R.anim.anim_slide_in_right,R.anim.anim_slide_out_left);
							finish();
						}
					}
					break;
				}
				case 2:{//疫情上报
					if(father==MODULE_FATHER){
						Intent editprofile = new Intent();
						Bundle bd = new Bundle();
						bd.putInt("Profile_id", profileId);//待编辑档案的ID
						bd.putInt("father", MODULE_CHILD);
						bd.putInt("call", MODULE_PEOPLE_HEALTH);
						editprofile.putExtras(bd);
						editprofile.setClass(VaccinationScrollActivity.this, EpidemicList.class);
						startActivity(editprofile);
						overridePendingTransition(R.anim.anim_slide_in_right,R.anim.anim_slide_out_left);
					}
					else if(father==MODULE_CHILD){
						if(call==MODULE_CHILD_MANAGEMENT){
							finish();
							overridePendingTransition(R.anim.anim_slide_in_left,R.anim.anim_slide_out_right);
						}
						else{
							Intent editprofile = new Intent();
							Bundle bd = new Bundle();
							bd.putInt("Profile_id", profileId);//待编辑档案的ID
							bd.putInt("father", MODULE_CHILD);
							bd.putInt("call", call);
							editprofile.putExtras(bd);
							editprofile.setClass(VaccinationScrollActivity.this, EpidemicList.class);
							startActivity(editprofile);
							overridePendingTransition(R.anim.anim_slide_in_right,R.anim.anim_slide_out_left);
							finish();
						}
					}
					break;
				}
				case 3:{//卫生监督
					
					break;
				}
				case 4:{//寓教于乐
					if(father==MODULE_FATHER){
						Intent editprofile = new Intent();
						Bundle bd = new Bundle();
						bd.putInt("Profile_id", profileId);//待编辑档案的ID
						bd.putInt("father", MODULE_CHILD);
						bd.putInt("call", MODULE_PEOPLE_HEALTH);
						editprofile.putExtras(bd);
						editprofile.setClass(VaccinationScrollActivity.this, YuleActivity.class);
						startActivity(editprofile);
						overridePendingTransition(R.anim.anim_slide_in_right,R.anim.anim_slide_out_left);
					}
					else if(father==MODULE_CHILD){
						if(call==MODULE_CHILD_MANAGEMENT){
							finish();
							overridePendingTransition(R.anim.anim_slide_in_left,R.anim.anim_slide_out_right);
						}
						else{
							Intent editprofile = new Intent();
							Bundle bd = new Bundle();
							bd.putInt("Profile_id", profileId);//待编辑档案的ID
							bd.putInt("father", MODULE_CHILD);
							bd.putInt("call", call);
							editprofile.putExtras(bd);
							editprofile.setClass(VaccinationScrollActivity.this, YuleActivity.class);
							startActivity(editprofile);
							overridePendingTransition(R.anim.anim_slide_in_right,R.anim.anim_slide_out_left);
							finish();
						}
					}
					break;
				}
					
				default:
					break;
				}
			}

		});
        
        mpDialog=new ProgressDialog(VaccinationScrollActivity.this);
        mpDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        mpDialog.setCancelable(true);
        mpDialog.setMessage("正在加载...");
        mpDialog.setIndeterminate(false);
        mpDialog.show();
        new myThread(handler).start();
        
        
	}
	private void initList() {
		// TODO Auto-generated method stub
		ArrayList<Map<String,Object>>coll=new ArrayList<Map<String,Object>>();
		Map<String,Object>item=new HashMap<String,Object>();
		item.put("name", "预防接种卡");
		coll.add(item);
		item=new HashMap<String,Object>();
		item.put("name", "预防接种记录表");
		coll.add(item);
		item=new HashMap<String,Object>();
		item.put("name", "预防接种记录续表");
		coll.add(item);
		listAccount.setAdapter(new SimpleAdapter(this,coll,R.layout.member_listview, new String[] { "name" },new int[] {R.id.txtFather}));
		listAccount.setDivider(null);
	}
	private void initViews() {
		// TODO Auto-generated method stub
		mRoot.removeAllViews();
		vaca=new VaccinationCardView(this,this.profileId);
		vare=new VaccinationRecordView(this,this.profileId);
		vare2=new VaccinationRecordView2(this,this.profileId);
		mRoot.addView(vaca);
		mRoot.addView(vare);
		mRoot.addView(vare2);
	}
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if(v==leftBtn){
			new AlertDialog.Builder(VaccinationScrollActivity.this)
			.setIcon(R.drawable.alert_dialog_icon)
			.setTitle("您确定已经保存了要退出吗？")
			.setPositiveButton("确定", new DialogInterface.OnClickListener(){
				public void onClick(DialogInterface dialog, int whichButton) {
					VaccinationScrollActivity.this.finish();
					overridePendingTransition(R.anim.anim_slide_in_left,R.anim.anim_slide_out_right);
				}
			})
			.setNegativeButton("取消", new DialogInterface.OnClickListener() {
		                    public void onClick(DialogInterface dialog, int whichButton) {

		                        /* User clicked Cancel so do some stuff */
		                   }
		                })
		                .create().show();
		}
		if(v==rightBtn){  //保存按钮
			pageIndex=mRoot.getCurScreen();
			int i=0;
			boolean result=false;
			switch(pageIndex){
			case 0:
				result=vaca.saveVaccinationCard();
				i=0;
				break;
			case 1:
				result=vare.saveVaccinationRecordView()&&vare2.saveVaccinationRecordView2();
				i=1;
				break;
			case 2:
				result=vare.saveVaccinationRecordView()&&vare2.saveVaccinationRecordView2();
				i=1;
				break;		
			}
			if(result){
				Toast.makeText(this,vaccinationTable[i]+"表保存成功！", Toast.LENGTH_LONG).show();	
			}else{
				Toast.makeText(this, vaccinationTable[i]+"表保存失败！", Toast.LENGTH_LONG).show();
			}
		}	
	}

	public class myThread extends Thread{
		private Handler handler;
		public myThread(Handler handler){
			this.handler=handler;
		}
		public void run(){
			try{
				Looper.getMainLooper();
				sleep(300);
				vaca.selectData();
				vare.findWidget();
				vare.selectData();
				vare2.findWidget();
				vare2.selectData();

				Message msg=handler.obtainMessage();
				msg.what=1;
				handler.sendMessage(msg);
				
			}catch(Exception e){
				e.printStackTrace();
			}	
		}
	
	}
	private final Handler handler = new Handler(Looper.getMainLooper()){
		public void handleMessage(Message msg){
			if(msg.what==1){
				vaca.initVaccinationCard();
				vare.addToView();
				vare.initVaccinationRecordView();
				vare2.addToView();
				vare2.initVaccinationRecordView2();
				
				mpDialog.cancel();
			}
		}

	};
}

